package com.pic.mgt.monitor.backup;

import java.io.File;
import java.io.IOException;

import com.pic.mgt.log.IgxLog;
import com.pic.mgt.unit.Message;
import com.pic.mgt.unit.Unit;
import com.pic.mgt.unit.operation.MessageUnitOperation;
import com.pic.mgt.unit.operation.UnitOperationException;
import com.pic.mgt.utils.LogUtils;

public class MessagePhysicalBackup extends UnitPhysicalBackup {
	protected MessageUnitOperation simpleMessageUnitOperation;

	@Override
	/**
	 * Message Unit 備份不包含 Transaction File 備份，僅備份確認檔。
	 */
	public boolean backupUnit(Unit unit) {
		// 取得包含正確日期的備份路徑位置
		File backupDestFolder = super.getBackupUnitPath();
		
		return backupUnit(unit, backupDestFolder);
	}

	@Override
	public boolean backupUnit(Unit unit, File backupDestFolder) {
		/*
		 * Message 的備份不需要包含備份 transaction file。
		 */
		
		Message message = (Message)unit;
		
		boolean hasCompleteAction = true;
		
		// 開始備份實體檔案
		
		// 備份 trans ok 確認檔
		try {
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this,
						"Backuping message '{}' confirmation file '{}'.", message
								.getUnitName(),
								message.getConfirmationFilename());
			}
			// ********************** Log **********************
			
			simpleMessageUnitOperation.copyConfirmationFile(
					message, backupDestFolder);
			
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this,
						"Backuping message '{}' confirmation file '{}' completed.", message
								.getUnitName(),
								message.getConfirmationFilename());
			}
			// ********************** Log **********************
		} catch (UnitOperationException e) {
			hasCompleteAction = false;
			
			// ********************** Log **********************
			IgxLog.messageLog.error(this,
					"SUDA-264 Backuping message '{}' confirmation file '{}' failed.", message
							.getUnitName(),
							message.getConfirmationFilename());
			// ********************** Log **********************
		}
		
		return hasCompleteAction;
	}

	@Override
	public boolean removeAndBackupErrorUnit(File confirmationFile) {
		boolean hasCompletedAction = true;
		
		// 取得包含正確日期的備份路徑位置
		File backupBadPath = super.getBackupBadUnitPath();
		
		// 開始搬移錯誤的實體檔案
		
		// 搬移確認檔案
		try {
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this,
						"Backuping wrong message: confirmation file '{}'.",
						confirmationFile.getName());
			}
			// ********************** Log **********************
			
			// throws IOException
			simpleFileOperation.moveFileToFolder(confirmationFile, backupBadPath);
			
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this,
						"Backuping wrong message: confirmation file '{}' completed.",
						confirmationFile.getName());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			hasCompletedAction = false;
			
			// ********************** Log **********************
			LogUtils.error(this, IgxLog.messageDetailsLog, e);
			// ********************** Log **********************
			
			// ********************** Log **********************
			IgxLog.messageLog.error(this,
					"SUDA-265 Backuping wrong message: confirmation file '{}' failed.",
					confirmationFile.getName());
			// ********************** Log **********************
		}
		
		return hasCompletedAction;
	}
	
	
	// --- getter and setter ---

	public void setSimpleMessageUnitOperation(
			MessageUnitOperation simpleMessageUnitOperation) {
		this.simpleMessageUnitOperation = simpleMessageUnitOperation;
	}

}
